package cpa.prowon.gui.actions;

import java.util.Iterator;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;

import cpa.prowon.Util;
import cpa.prowon.gui.MainWindow;
import cpa.prowon.image.ImageContainer;

/**
 * This action is used to copy the files names of the selected images to the clipboard
 * @author Chris
 */
public class CopyFileNamesToClipboardAction
	extends PatchedAction
	implements ISelectionChangedListener {

	/**
	 * Constructor for CopyFileNamesToClipboardAction
	 * @param MainWindow The window where the action is used
	 * @author Chris
	 */
	public CopyFileNamesToClipboardAction(MainWindow w) {
		super(w);
		setEnabled(false);
	}

	/**
	 * Return the title key
	 * @return The title key
	 * @author Chris
	 */
	protected String getTitleKey() {
		return "menu_item_edit_copy";
	}

	/**
	 * Return the tooltip key
	 * @return The tooltip key
	 * @author Chris
	 */
	protected String getTooltipKey() {
		return "menu_item_edit_copy_tooltip";
	}

	/**
	 * Return the image key
	 * @return The image key
	 * @author Chris
	 */
	protected String getImageKey() {
		return "file:icons/copy.gif";
	}
	
	/**
	 * Run the action
	 * @author Chris
	 */
	public void run() {
		
		// Get the clipboard and prepare a text transfer to it
	    Clipboard clipboard = Util.getClipboard();
	    TextTransfer text_transfer = TextTransfer.getInstance();
    
    	// Get the selected image
	    IStructuredSelection selection = window.getPictureListSelection();
	    if (selection.isEmpty()) {
	      return;
    	}

		// Get the file names
	    StringBuffer string_buffer = new StringBuffer();
	    for (Iterator i = selection.iterator(); i.hasNext();) {
	      ImageContainer id = (ImageContainer) i.next();
    	  string_buffer.append(" ");
	      string_buffer.append(id.getPathName());
    	}

		// Put the text into the clipboard
	    clipboard.setContents(
	      new Object[] { string_buffer.toString()},
    	  new Transfer[] { text_transfer });

	}
	  
	/**
	 * Listen to the selection changed event in order to enable or disable the action
	 * @param SelectionChangedEvent The event that occurs when the selection changed
	 * @author Chris
	 * 
	 */
	public void selectionChanged(SelectionChangedEvent event) {

		// Get the selection and set the action enabled or disabled depending on this
		IStructuredSelection selection = window.getPictureListSelection();
		if (selection.size() == 0) {
			setEnabled(false);
			return;
		}
		setEnabled(true);

	}
	
}

